package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.dao.CdnDomainInfoDao;

@Repository
public class CdnDomainInfoDaoImpl implements CdnDomainInfoDao {

	private static final Log log = LogFactory.getLog(CdnDomainInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCountStatistics(param);
		list = filter(listPageDataStatistics(param));
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	@Override
	public List<Map<String, Object>> getDetailInfo(IMap param) {
		String domainId = param.getString("domainId");
		String customerId = param.getString("customerId");
		String serviceId = param.getString("serviceId");
		String cdnId = param.getString("cdnId");
		StringBuffer sql = new StringBuffer();
		sql.append("select t.domainId,t.domain,t.regId,(select count(*) from t_cdn_cdnNetInfo t1 where t1.domainId = t.domainId and t1.cdnId = t.cdnId) as cdnNetNum,t.timstamp as reportTime from t_cdn_customer_service_domain t ");
		sql.append(" where domainId = '"+domainId+"' and customerId = '"+customerId+"' and serviceId = '"+serviceId+"' and cdnId = '"+cdnId+"' limit 1");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public List<Map<String, Object>> listPageDataStatistics(Map<String, Object> param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			sql.append("select province,domainId,domain,cdnName,unitName,cdnNetNum,regId,reportTime,cdnId,customerId,serviceId from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province='"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
		}else {
			sql.append("select province,domainId,domain,cdnName,unitName,cdnNetNum,regId,reportTime,cdnId,customerId,serviceId from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province='"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" union all");
			sql.append(" select province,domainId,domain,cdnName,unitName,cdnNetNum,regId,reportTime,cdnId,customerId,serviceId from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province !='"+userProvince+"'");
			sql.append(" and domain.cdnId in (select distinct house.cdnId from t_cdn_house_statistics house where house.houseProvince = '"+userProvince+"')");
			sql.append(getQueryConfStatistics(param));
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCountStatistics(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			sql.append("select count(1) count from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province='"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
		}else {
			sql.append("select sum(count) count from (");
			sql.append(" select count(1) count from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province='"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" union all");
			sql.append(" select count(1) count from t_cdn_domain_statistics domain where 1 = 1");
			sql.append(" and domain.province !='"+userProvince+"'");
			sql.append(" and domain.cdnId in (select distinct house.cdnId from t_cdn_house_statistics house where house.houseProvince = '"+userProvince+"')");
			sql.append(getQueryConfStatistics(param));
			sql.append(") t");
		}
		
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			sql = getSql();
			sql.append(getQueryConf(param));
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		}else {//高级权限
			sql.append("select * from (");
			sql.append(getSql());
			sql.append(" and cdn.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			//sql.append(" and exists (select cdn.cdnId from t_cdn_cdnNet_node_house house where cdn.cdnId = house.cdnId and house.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(" and cdn.province !='"+userProvince+"'");
			sql.append(" and customer.province='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		}
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public StringBuffer getSql() {
		StringBuffer sql = new StringBuffer();
		sql.append("select domain.domainId,domain.domain,cdn.cdnName,customer.unitName,domain.regId,domain.timstamp as reportTime,");
		sql.append("	   0 as ipNum,(select count(*) from t_cdn_cdnNetInfo t1 where t1.domainId = domain.domainId and t1.cdnId = domain.cdnId) as cdnNetNum,domain.cdnId,domain.customerId,domain.serviceId,customer.province");
		sql.append("  from t_cdn_customer_service_domain domain,t_cdn_customerinfo customer,t_cdn_cdndata cdn");
		sql.append(" where domain.customerId = customer.customerId");
		sql.append(" and cdn.cdnId = domain.cdnId");
		sql.append(" and cdn.cdnId = customer.cdnId");
		return sql;
	}
	
	public StringBuffer getSqlCount() {
		StringBuffer sql = new StringBuffer();
		sql.append("select domain.domainId,domain.domain,cdn.cdnName,customer.unitName,domain.regId,domain.timstamp");
		sql.append("  from t_cdn_customer_service_domain domain,t_cdn_customerinfo customer,t_cdn_cdndata cdn");
		sql.append(" where domain.customerId = customer.customerId");
		sql.append(" and cdn.cdnId = domain.cdnId");
		sql.append(" and cdn.cdnId = customer.cdnId");
		return sql;
	}
	
	public String listCount(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "3")) {
			sql.append("select count(*) count from (");
			sql.append(getSqlCount());
			sql.append(" and cdn.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSqlCount());
			//sql.append(" and exists (select cdn.cdnId from t_cdn_cdnNet_node_house house where cdn.cdnId = house.cdnId and house.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(" and cdn.province !='"+userProvince+"'");
			sql.append(" and customer.province='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else {
			sql.append("select count(*) count from(");
			sql.append(getSqlCount());
			sql.append(getQueryConf(param));
			sql.append(") t");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConfStatistics(Map<String,Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String linkCustomerId = CommonUtils.valueOf(param.get("linkCustomerId"));
		String regId = CommonUtils.valueOf(param.get("regId"));
		String domain = CommonUtils.valueOf(param.get("domain"));
		String cdnName = CommonUtils.valueOf(param.get("cdnName"));
		String cdnId = CommonUtils.valueOf(param.get("cdnId"));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(linkCustomerId)) {
			queryConf.append(" and domain.customerId='"+linkCustomerId+"'");
		}
		if(StringUtils.isNotBlank(linkCdnNetId) && StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and exists (select net.domainId from t_cdn_cdnnetinfo net where net.cdnNetId='"+linkCdnNetId+"' and net.cdnId='"+cdnId+"' and net.domainId = domain.domainId)");
		}
		if(StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and domain.cdnId = '"+cdnId+"'");
		}
		if(StringUtils.isNotBlank(regId)) {
			queryConf.append(" and domain.regId like '%"+regId+"%'");
		}
		if(StringUtils.isNotBlank(domain)) {
			queryConf.append(" and domain.domain like '%"+domain+"%'");
		}
		if(StringUtils.isNotBlank(cdnName)) {
			queryConf.append(" and domain.cdnName like '%"+cdnName+"%'");
		}
		return queryConf;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String linkCustomerId = CommonUtils.valueOf(param.get("linkCustomerId"));
		String regId = CommonUtils.valueOf(param.get("regId"));
		String domain = CommonUtils.valueOf(param.get("domain"));
		String cdnName = CommonUtils.valueOf(param.get("cdnName"));
		String cdnId = CommonUtils.valueOf(param.get("cdnId"));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer queryConf = new StringBuffer();
		
		if(!StringUtils.equals(userLevel, "3")) {
			queryConf.append(" and cdn.province ='"+userProvince+"'");
			if(StringUtils.equals(userLevel, "1")) {//初级权限
				queryConf.append(" and customer.province='"+userProvince+"'");
			}else if(StringUtils.equals(userLevel, "2")) {//中级权限
				//中级权限企业下资源不需要限制
			}
		}
		if(StringUtils.isNotBlank(linkCustomerId)) {
			queryConf.append(" and domain.customerId='"+linkCustomerId+"'");
		}
		if(StringUtils.isNotBlank(linkCdnNetId) && StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and exists (select net.domainId from t_cdn_cdnnetinfo net where net.cdnNetId='"+linkCdnNetId+"' and net.cdnId='"+cdnId+"' and net.domainId = domain.domainId)");
		}
		if(StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and domain.cdnId = '"+cdnId+"'");
		}
		if(StringUtils.isNotBlank(regId)) {
			queryConf.append(" and domain.regId like '%"+regId+"%'");
		}
		if(StringUtils.isNotBlank(domain)) {
			queryConf.append(" and domain.domain like '%"+domain+"%'");
		}
		if(StringUtils.isNotBlank(cdnName)) {
			queryConf.append(" and cdn.cdnName like '%"+cdnName+"%'");
		}
		return queryConf;
	}
	public List<Map<String, Object>> filter(List<Map<String, Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				map.put("cdnNetNum",CommonUtils.filteNull(map.get("cdnNetNum")));
			}
		}
		return list;
	}
}
